package com.spyx.product.controller;


import com.atyx.spyx.model.product.SkuInfo;
import com.atyx.spyx.vo.product.SkuInfoQueryVo;
import com.atyx.spyx.vo.product.SkuInfoVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spyx.product.service.SkuInfoService;
import com.spyx.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * sku信息 前端控制器
 * </p>
 *
 * @author yhp
 * @since 2024-05-15
 */
@RestController
@RequestMapping("/admin/product/skuInfo")
//@CrossOrigin
@Api(tags = "sku管理")
public class SkuInfoController {
    @Autowired
    private SkuInfoService skuInfoService;

    @ApiOperation("sku分页")
    @GetMapping("{page}/{limit}")
    public Result getList(
            @PathVariable("page") Integer pageNum,
            @PathVariable("limit")Integer limit,
            SkuInfoQueryVo queryVo
    ){
        Page page = new Page(pageNum, limit);
        IPage<SkuInfo> pageModel= skuInfoService.getInfoList(page,queryVo);
        return Result.ok(pageModel);
    }


    @ApiOperation("保存")
    @PostMapping("save")   //遇到前端数据和后端实体类的属性不匹配，解决方案:单独在创建一个vo类
    public Result save(@RequestBody SkuInfoVo skuInfoVo){
        skuInfoService.saveSkuInfo(skuInfoVo);
        return Result.ok();
    }

    @ApiOperation("主键查询")
    @GetMapping("get/{id}")
    public Result getById(@PathVariable("id")Integer id){
       SkuInfoVo vo= skuInfoService.getSkuInfoById(id);
        return Result.ok(vo);
    }
    //更新
    @ApiOperation("更新sku")
    @PutMapping("update")
    public Result update(@RequestBody SkuInfoVo vo){
        skuInfoService.updateSkuInfo(vo);
        return Result.ok();
    }

    //主键删除
    @ApiOperation("sku主键删除")
    @DeleteMapping("/remove/{id}")
    public Result delete(@PathVariable("id")Integer id){
        skuInfoService.removeById(id);
        return Result.ok();
    }

    //批量删除
    @ApiOperation("sku批量删除")
    @DeleteMapping("/batchRemove")
    public Result deleteByIds(@RequestBody List<Integer> ids){
        skuInfoService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation("上下架")
    @GetMapping("/publish/{id}/{status}")
    public Result publish(@PathVariable("id")Long skuId,
                          @PathVariable("status")Long status){
        System.out.println("skuid="+skuId+",status="+status);
        skuInfoService.publish(skuId,status);
        return Result.ok();
    }

    @ApiOperation("商品审核")
    @GetMapping("/check/{id}/{status}")
    public Result check(@PathVariable("id")Long skuId,
                          @PathVariable("status")Long status){
        skuInfoService.check(skuId,status);
        return Result.ok();
    }

    @ApiOperation("新人专享")
    @GetMapping("/isNewPerson/{id}/{status}")
    public Result isNewPerson(@PathVariable("id")Long skuId,
                        @PathVariable("status")Long status){
        skuInfoService.isNewPerson(skuId,status);
        return Result.ok();
    }


}

